//
//  DataBaseHandler.m
//  MoGuRadio
//
//  Created by dlios on 15/6/11.
//  Copyright (c) 2015年 mogu.com 蘑菇科技. All rights reserved.
//

#import "DataBaseHandler.h"
#import "MushRoom.h"

@implementation DataBaseHandler


+ (DataBaseHandler *)shareInstance
{
    
    // 声明一个静态的指针, 只会在第一次调用这个方法的时候被创建.
    static DataBaseHandler *dbHandler = nil;
        // 如果指针指向nil, 说明是第一次调用的这个方法
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dbHandler = [[DataBaseHandler alloc] init];
        [dbHandler openDB];
        [dbHandler creatTable];
        
        NSLog(@"方法只走一次");
        
    });
    
    return dbHandler;
}

// 打开数据库
- (BOOL)openDB
{
    
    // 在沙盒中创建一个db文件路径
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    
    NSString *path = [docPath stringByAppendingPathComponent:@"mogu.db"];
    
    int result = sqlite3_open([path UTF8String], &dbPoint);
    
    return [self judgeResult:result withTitle:@"打开数据库"];
    
    
}

// 关闭数据库
- (BOOL)closeDB
{
    int result = sqlite3_close(dbPoint);
    
    return [self judgeResult:result withTitle:@"关闭数据库"];
}


// 封装一个方法, 来判断数据库的执行结果
- (BOOL)judgeResult:(int)result withTitle:(NSString *)title
{
    
    if (result == SQLITE_OK) {
        
        return YES;
        
    } else {
        
        NSLog(@"%@失败, 错误原因: %d", title, result);
        
        return NO;
   }
    
}


// 创建表
- (BOOL)creatTable
{
    
    NSString *sql = [NSString stringWithFormat:@"create table mushRoom (pic text, focusLabelText text, listenLabelText text, titleLabelText text, desLabelText text, hostNameText text, rid text primary key)"];
    
    int result = sqlite3_exec(dbPoint, [sql UTF8String], NULL, NULL, NULL);
    
    return [self judgeResult:result withTitle:@"创建表"];
    
}


- (BOOL)insertMushRoom:(MushRoom *)mushRoom
{
    // 添加一条数据
    NSString *sql = [NSString stringWithFormat:@"insert into mushRoom values ('%@', '%@', '%@', '%@', '%@', '%@', '%@')", mushRoom.pic, mushRoom.focusLabelText, mushRoom.listenLabelText, mushRoom.titleLabelText, mushRoom.desLabelText, mushRoom.hostNameText, mushRoom.rid];
    
    int result = sqlite3_exec(dbPoint, [sql UTF8String], NULL, NULL, NULL);
    return [self judgeResult:result withTitle:@"添加一条数据"];
    
}


- (BOOL)deleteMushRoomWithRid:(NSString *)rid
{
    // 删除一条数据
    NSString *sql = [NSString stringWithFormat:@"delete from mushRoom where rid=%@", rid];
    int result = sqlite3_exec(dbPoint, [sql UTF8String], NULL, NULL, NULL);
    return [self judgeResult:result withTitle:@"删除一条数据"];
}


- (NSMutableArray *)selectAll
{
    // 查询所有数据
    sqlite3_stmt *stmt = nil;
    NSString *sql = [NSString stringWithFormat:@"select * from mushRoom"];
    int result = sqlite3_prepare_v2(dbPoint, [sql UTF8String], -1, &stmt, NULL);
    
    // 提前创建好一个空的可变数组
    NSMutableArray *tempArr = [NSMutableArray array];
    if (result == SQLITE_OK) {
        
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            
            MushRoom *mushRoom = [[MushRoom alloc] init];
            [tempArr addObject:mushRoom];
            [mushRoom release];
            
            const unsigned char *picChar = sqlite3_column_text(stmt, 0);
            const unsigned char *focusLabelTextChar = sqlite3_column_text(stmt, 1);
            const unsigned char *listenLabelTextChar = sqlite3_column_text(stmt, 2);
            const unsigned char *titleLabelTextChar = sqlite3_column_text(stmt, 3);
            const unsigned char *desLabelTextChar = sqlite3_column_text(stmt, 4);
            const unsigned char *hostNameTextChar = sqlite3_column_text(stmt, 5);
            const unsigned char *ridChar = sqlite3_column_text(stmt, 6);
            
            NSString *pic = [NSString stringWithUTF8String:picChar];
            NSString *focusLabelText = [NSString stringWithUTF8String:focusLabelTextChar];
            NSString *listenLabelText = [NSString stringWithUTF8String:listenLabelTextChar];
            NSString *titleLabelText = [NSString stringWithUTF8String:titleLabelTextChar];
            NSString *desLabelText = [NSString stringWithUTF8String:desLabelTextChar];
            NSString *rid = [NSString stringWithUTF8String:ridChar];
            NSString *hostNameText = [NSString stringWithUTF8String:hostNameTextChar];

            
            mushRoom.pic = pic;
            mushRoom.rid = rid;
            mushRoom.focusLabelText = focusLabelText;
            mushRoom.listenLabelText = listenLabelText;
            mushRoom.titleLabelText = titleLabelText;
            mushRoom.desLabelText = desLabelText;
            mushRoom.hostNameText = hostNameText;
            
        }
    }
    
    sqlite3_finalize(stmt);
    
    return tempArr;
}


@end
